package com.sc.nft.service.impl;

import com.sc.nft.dao.ExpenditureRecordDao;
import com.sc.nft.dao.UserInfoDao;
import com.sc.nft.dao.UserWalletDao;
import com.sc.nft.dao.UserWalletDetailDao;
import com.sc.nft.entity.ExpenditureRecord;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.UserWallet;
import com.sc.nft.entity.UserWalletDetail;
import com.sc.nft.entity.dto.OperateUserWalletBatchAmountDTO;
import com.sc.nft.entity.dto.OperateUserWalletBatchDTO;
import com.sc.nft.entity.dto.UpdateUserByIdBatchDTO;
import com.sc.nft.enums.ExpenditureRecordTypeEnum;
import com.sc.nft.enums.OperateUserWalletEnum;
import com.sc.nft.enums.UserBalanceTypeEnum;
import com.sc.nft.enums.WalletDetailTypeEnum;
import com.sc.nft.service.UserWalletAdminBatchService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class UserWalletAdminBatchSaveSubtractPayBalanceServiceImpl implements UserWalletAdminBatchService {

    private final UserInfoDao userInfoDao;

    private final UserWalletDao userWalletDao;

    private final UserWalletDetailDao userWalletDetailDao;

    private final ExpenditureRecordDao expenditureRecordDao;

    /**
     * 批量新增用户可提现余额的实现
     *
     * @param operateUserWalletBatchDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean operateUserWalletBatch(OperateUserWalletBatchDTO operateUserWalletBatchDTO) {
        // 用户加钱数据列表
        List<OperateUserWalletBatchAmountDTO> userMoney = operateUserWalletBatchDTO.getUserMoney();
        for (OperateUserWalletBatchAmountDTO data : userMoney) {
            // 用户的余额修改日志详情
            List<UserWalletDetail> userWalletDetails = new ArrayList<>();
            // 充值记录
            List<ExpenditureRecord> expenditureRecords = new ArrayList<>();
            // 用户的钱包增加额度
            List<UpdateUserByIdBatchDTO> updateUserByIdBatchs = new ArrayList<>();
            UserInfo userInfo = userInfoDao.getOneByUserTel(data.getUserTel());
            UserWallet byUserId = userWalletDao.getByUserId(userInfo.getId());
            BigDecimal balance = byUserId.getPayBalance();
            // 记录用户的金额日志
            ExpenditureRecord expenditureRecord = new ExpenditureRecord();
            expenditureRecord.setUserId(userInfo.getId());
            expenditureRecord.setAmount(data.getAmount());
            expenditureRecord.setCharge(BigDecimal.ZERO);
            expenditureRecord.setRealAmount(data.getAmount());
            expenditureRecord.setRemark(operateUserWalletBatchDTO.getRemark());
            expenditureRecord.setType(ExpenditureRecordTypeEnum.BACKSTAGE_SUB);
            expenditureRecord.setBeforeBalance(balance);
            expenditureRecord.setAfterBalance(balance.subtract(data.getAmount()));
            expenditureRecords.add(expenditureRecord);

            // 记录用户钱包日志
            UserWalletDetail userWalletDetail = new UserWalletDetail();
            userWalletDetail.setUserId(userInfo.getId());
            userWalletDetail.setAmount(data.getAmount());
            userWalletDetail.setCharge(BigDecimal.ZERO);
            userWalletDetail.setRealAmount(data.getAmount());
            userWalletDetail.setBeforeBalance(balance);
            userWalletDetail.setType(WalletDetailTypeEnum.BACKSTAGE_SUB);
            userWalletDetail.setAfterBalance(balance.subtract(data.getAmount()));
            userWalletDetail.setIsAdd(Boolean.FALSE);
            userWalletDetail.setRemark(operateUserWalletBatchDTO.getRemark());
            userWalletDetail.setBalanceType(UserBalanceTypeEnum.PAY_BALANCE);
            userWalletDetails.add(userWalletDetail);
            UpdateUserByIdBatchDTO updateUserByIdBatch = new UpdateUserByIdBatchDTO();

            // 用户钱包增加额度
            updateUserByIdBatch.setUserId(userInfo.getId());
            updateUserByIdBatch.setAmount(data.getAmount());
            updateUserByIdBatchs.add(updateUserByIdBatch);

            // 开始处理用户批量数据,2个批量新增 1个批量修改
            // 钱包日志
            userWalletDetailDao.saveBatch(userWalletDetails);
            // 日志
            expenditureRecordDao.saveBatch(expenditureRecords);
            // 修改用户钱包
            userWalletDao.updateUserBatch(updateUserByIdBatchs, operateUserWalletBatchDTO.getUserBalanceType().getCode());
        }
        return Boolean.TRUE;
    }

    @Override
    public OperateUserWalletEnum code() {
        return OperateUserWalletEnum.DEDUCT_PAY_BALANCE;
    }

}
